package com.zhangxu.microservice.auth.dao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangxu.microservice.auth.dao.condition.EmployeeRoleCondition;
import com.zhangxu.microservice.auth.dao.entity.EmployeeRoleDO;
import com.zhangxu.microservice.auth.dao.mapper.EmployeeRoleMapper;
import com.zhangxu.microservice.auth.dao.service.EmployeeRoleDao;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 员工角色关系 DAO 实现类
 */
@Service
public class EmployeeRoleDaoImpl extends ServiceImpl<EmployeeRoleMapper, EmployeeRoleDO> implements EmployeeRoleDao {

    @Override
    public List<EmployeeRoleDO> getByEmployeeId(Long employeeId) {
        LambdaQueryWrapper<EmployeeRoleDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EmployeeRoleDO::getEmployeeId, employeeId)
                .eq(EmployeeRoleDO::getDeleteTime, 0);
        return list(queryWrapper);
    }

    @Override
    public List<EmployeeRoleDO> getByRoleId(Long roleId) {
        LambdaQueryWrapper<EmployeeRoleDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EmployeeRoleDO::getRoleId, roleId)
                .eq(EmployeeRoleDO::getDeleteTime, 0);
        return list(queryWrapper);
    }

    @Override
    public EmployeeRoleDO getByEmployeeIdAndRoleId(Long employeeId, Long roleId) {
        LambdaQueryWrapper<EmployeeRoleDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EmployeeRoleDO::getEmployeeId, employeeId)
                .eq(EmployeeRoleDO::getRoleId, roleId)
                .eq(EmployeeRoleDO::getDeleteTime, 0);
        return getOne(queryWrapper);
    }

    @Override
    public IPage<EmployeeRoleDO> page(EmployeeRoleCondition condition) {
        LambdaQueryWrapper<EmployeeRoleDO> queryWrapper = new LambdaQueryWrapper<>();
        
        if (condition.getEmployeeId() != null) {
            queryWrapper.eq(EmployeeRoleDO::getEmployeeId, condition.getEmployeeId());
        }
        
        if (condition.getRoleId() != null) {
            queryWrapper.eq(EmployeeRoleDO::getRoleId, condition.getRoleId());
        }
        
        queryWrapper.eq(EmployeeRoleDO::getDeleteTime, 0)
                .orderByDesc(EmployeeRoleDO::getCreateTime);
        queryWrapper.orderByDesc(EmployeeRoleDO::getId);
        
        return page(condition.getIpage(), queryWrapper);
    }

    @Override
    public void deleteByEmployeeId(Long employeeId) {
        LambdaUpdateWrapper<EmployeeRoleDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(EmployeeRoleDO::getEmployeeId, employeeId)
                .eq(EmployeeRoleDO::getDeleteTime, 0)
                .set(EmployeeRoleDO::getDeleteTime, System.currentTimeMillis());
        update(updateWrapper);
    }

    @Override
    public void deleteByRoleId(Long roleId) {
        LambdaUpdateWrapper<EmployeeRoleDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(EmployeeRoleDO::getRoleId, roleId)
                .eq(EmployeeRoleDO::getDeleteTime, 0)
                .set(EmployeeRoleDO::getDeleteTime, System.currentTimeMillis());
        update(updateWrapper);
    }
}